package com.maimang.platform.dsp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.maimang.platform.dsp.constant.AdvertiserApi;
import com.maimang.platform.dsp.entity.AdvertiserIds;
import com.maimang.platform.dsp.entity.Fund;
import com.maimang.platform.dsp.mapper.FundMapper;
import com.maimang.platform.dsp.service.AccessTokenService;
import com.maimang.platform.dsp.service.AdvertiserIdsService;
import com.maimang.platform.dsp.service.FundService;
import com.maimang.platform.dsp.utils.ApiService;
import com.maimang.platform.lang.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname FundServiceImpl
 * @description <br>账户余额 服务层实现</br>
 * @date 2019-07-31 10:07:36
 * @email jintaozhao@qq.com
 */
@Service
public class FundServiceImpl implements FundService {

	@Autowired
	private FundMapper fundMapper;

	@Autowired
	private ApiService apiService;

	@Autowired
	private AccessTokenService accessTokenService;

	@Autowired
	private AdvertiserIdsService advertiserIdsService;

	/**
	 * 查询账户余额信息
	 *
	 * @param advertiserId 账户余额ID
	 * @return 账户余额信息
	 */
	@Override
	public Fund syncFundById(Long advertiserId) {
		Fund fund = null;
		try {
			fund = this.getAdvertiserFund(advertiserId);
			fundMapper.deleteFundById(advertiserId);
			fundMapper.insertFund(fund);
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fund;
	}

	/**
	 * 同步代理商下所有广告主账户余额
	 *
	 * @param advertiserId 代理商广告主ID
	 */
	@Override
	public void syncFund(String advertiserId) {
		List<AdvertiserIds> idsList = advertiserIdsService.selectAdvertiserIds(Long.valueOf(advertiserId));

		double total = idsList.size();
		int pageSize = 2000;
		double totalPage = Math.ceil(total / pageSize);
		for (int i = 0; i < totalPage; i++) {
			int beginIndex = i * pageSize;
			int endIndex = (int) Math.min((i + 1) * pageSize, total);

			Executors.newCachedThreadPool().execute(() -> {
				List<AdvertiserIds> advertiserIdsList = idsList.subList(beginIndex, endIndex);

				for (int j = 0; j < advertiserIdsList.size(); j++) {
					AdvertiserIds advertiser = advertiserIdsList.get(j);
					syncFundById(advertiser.getId());
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}
	}

	/**
	 * 查询账户余额列表
	 *
	 * @param fund 账户余额信息
	 * @return 账户余额集合
	 */
	@Override
	public List<Fund> selectFundList(Fund fund) {
		return fundMapper.selectFundList(fund);
	}

	/**
	 * 查询账户余额列表
	 *
	 * @param advertiserId 广告主id
	 * @return 账户余额集合
	 */
	@Override
	public List<Fund> findFundList(Long advertiserId) {
		Fund fund = null;
		if (advertiserId != null) {
			fund = new Fund();
			fund.setAdvertiserId(advertiserId);
		}
		return fundMapper.selectFundList(fund);
	}

	/**
	 * 新增账户余额
	 *
	 * @param fund 账户余额信息
	 * @return 结果
	 */
	@Override
	public int insertFund(Fund fund) {
		return fundMapper.insertFund(fund);
	}

	/**
	 * 修改账户余额
	 *
	 * @param fund 账户余额信息
	 * @return 结果
	 */
	@Override
	public int updateFund(Fund fund) {
		return fundMapper.updateFund(fund);
	}

	/**
	 * 删除账户余额对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteFundByIds(String ids) {
		return fundMapper.deleteFundByIds(ConvertUtils.toStrArray(ids));
	}

	/**
	 * 查询账号余额
	 *
	 * @param advertiserId
	 * @return
	 */
	@Override
	public Fund getAdvertiserFund(Long advertiserId) {
		Fund fund = new Fund();
		Map param = new HashMap();
		param.put("advertiser_id", advertiserId);
		String accessToken = accessTokenService.accessToken();
		JSONObject result = apiService.sendGet(AdvertiserApi.FUND_GET, accessToken, param);

		if (result != null) {
			if (result.getInteger("code") == 0) {
				JSONObject data = result.getJSONObject("data");
				fund = data.toJavaObject(Fund.class);
			}
		}
		return fund;
	}

}
